/* Pically
 * Copyright (C) 2011 Petros Dolaschjan.  All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package de.pd.pically.gui.dialog;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import net.miginfocom.swing.MigLayout;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;

import de.pd.pically.bean.CalendarEventBean;
import de.pd.pically.bean.CalendarEventBean.EventType;
import de.pd.pically.util.IcalUtil;
import de.pd.pically.util.UserSession;

/**
 * @author petros
 * 
 */
@SuppressWarnings("serial")
public class ImportIcalDialog extends JDialog {

	private JComboBox mCbYears;
	private JComboBox mEventTypeCombo;
	private JLabel mLblAmount;
	private JButton mBtnImport;

	/**
	 * Create the dialog.
	 */
	public ImportIcalDialog(JFrame pParent) {
		super(pParent, true);
		setResizable(false);
		setTitle("Import iCal File");
		setSize(new Dimension(300, 200));
		setLocationRelativeTo(pParent);
		ApplicationActionMap actionMap = Application.getInstance().getContext()
				.getActionMap(this);

		setLayout(new MigLayout("wrap 2"));

		add(new JLabel("Year"));
		Integer[] years = new Integer[] { 2011, 2012, 2013, 2014, 2015 };
		mCbYears = new JComboBox(years);
		add(mCbYears);

		add(new JLabel("Event Type"));
		mEventTypeCombo = new JComboBox(CalendarEventBean.getAllEventTypes());
		add(mEventTypeCombo);

		add(new JLabel("iCal File"));
		JButton btnBrowse = new JButton(actionMap.get("browseAction"));
		btnBrowse.setText("Browse");
		add(btnBrowse);
		
		mLblAmount = new JLabel("");
		add(mLblAmount, "span 2");
		
		mBtnImport = new JButton(actionMap.get("importAction"));
		mBtnImport.setText("Import Now");
		mBtnImport.setEnabled(false);
		add(mBtnImport, "span 2, center");
		
	}

	@Action
	public void browseAction() {
		JFileChooser jfc = new JFileChooser();
		jfc.setMultiSelectionEnabled(false);
		jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int selection = jfc.showOpenDialog(getParent());
		if (selection == JFileChooser.APPROVE_OPTION) {
			String ical = jfc.getSelectedFile().getAbsolutePath();

			ArrayList<CalendarEventBean> events = null;
			try {
				events = IcalUtil.getEventsFromIcal(ical,
						(Integer) mCbYears.getSelectedItem(), (EventType)mEventTypeCombo.getSelectedItem());
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, "iCal file could not be parsed! Please select a valid iCal file", "Import Error",
						JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
			}
			if(events != null && events.size() > 0) {
				mBtnImport.setEnabled(true);
				mLblAmount.setText("Found " + events.size() + " events");
				UserSession.lastImportEvents = events;
			}
		}
	}
	
	@Action
	public void importAction() {
		dispose();
	}

}
